package com.alex.insistence.web.cgform.upload;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.common.UploadFile;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.system.pojo.base.TSDocument;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.pojo.base.TSTypegroup;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/fileUpLoadController")
public class FileUploadController extends BaseController {
  /**
   * Logger for this class
   */
  private static final Logger logger = Logger.getLogger(FileUploadController.class);

  @Autowired
  private SystemService systemService;

  /**
   * 文件上传通用跳转
   * 
   * @param icon
   * @param req
   * @return
   */
  @RequestMapping(params = "commonUpload")
  public ModelAndView commonUpload(HttpServletRequest req) {
    return new ModelAndView("com/gomore/cards/web/cgform/upload/uploadView");
  }

  /**
   * 保存文件
   *
   * @param document
   * @return
   * @throws Exception
   */
  @RequestMapping(params = "saveFiles", method = RequestMethod.POST)
  @ResponseBody
  public AjaxJson saveFiles(HttpServletRequest request, HttpServletResponse response,
      TSDocument document) {
    AjaxJson j = new AjaxJson();
    Map<String, Object> attributes = new HashMap<String, Object>();
    TSTypegroup tsTypegroup = systemService.getTypeGroup("fieltype", "文档分类");
    TSType tsType = systemService.getType("files", "附件", tsTypegroup);
    String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID
    String documentTitle = oConvertUtils.getString(request.getParameter("documentTitle"));// 文件标题
    if (StringUtil.isNotEmpty(fileKey)) {
      document.setId(fileKey);
      document = systemService.getEntity(TSDocument.class, fileKey);
      document.setDocumentTitle(documentTitle);

    }
    document.setSubclassname(MyClassLoader.getPackPath(document));
    document.setCreatedate(DateUtils.gettimestamp());
    document.setTSType(tsType);
    UploadFile uploadFile = new UploadFile(request, document);
    uploadFile.setCusPath("files");
    // 设置weboffice转化【不设置该字段，则不做在线预览转化】
    uploadFile.setSwfpath("swfpath");
    document = systemService.uploadFile(uploadFile);
    attributes.put("url", document.getDownloadUrl());
    attributes.put("fileKey", document.getId());
    attributes.put("name", document.getAttachmenttitle());
    attributes.put("viewhref", "commonController.do?objfileList&fileKey=" + document.getId());
    attributes.put("delurl", "commonController.do?delObjFile&fileKey=" + document.getId());
    j.setMsg("文件添加成功");
    j.setAttributes(attributes);
    return j;
  }
}
